package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.system.domain.AlpContractDetail;
import com.ruoyi.system.domain.AlpContractStage;
import com.ruoyi.system.domain.AlpStageActual;
import com.ruoyi.system.service.IAlpContractDetailService;
import com.ruoyi.system.service.IAlpContractStageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AlpStageBudget;
import com.ruoyi.system.service.IAlpStageBudgetService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预算收款Controller
 * 
 * @author zhangjun
 * @date 2020-02-15
 */
@Controller
@RequestMapping("/system/budget")
public class AlpStageBudgetController extends BaseController
{
    private String prefix = "system/budget";

    @Autowired
    private IAlpStageBudgetService alpStageBudgetService;

    @Autowired
    private IAlpContractDetailService alpContractDetailService;

    @Autowired
    private IAlpContractStageService alpContractStageService;

    @RequiresPermissions("system:budget:view")
    @GetMapping()
    public String budget()
    {
        return prefix + "/budget";
    }

    /**
     * 查询预算收款列表
     */
    @RequiresPermissions("system:budget:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AlpStageBudget alpStageBudget)
    {
        startPage();
        List<AlpStageBudget> list = alpStageBudgetService.selectAlpStageBudgetList(alpStageBudget);
        return getDataTable(list);
    }

    /**
     * 导出预算收款列表
     */
    @RequiresPermissions("system:budget:export")
    @Log(title = "预算收款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AlpStageBudget alpStageBudget)
    {
        List<AlpStageBudget> list = alpStageBudgetService.selectAlpStageBudgetList(alpStageBudget);
        ExcelUtil<AlpStageBudget> util = new ExcelUtil<AlpStageBudget>(AlpStageBudget.class);
        return util.exportExcel(list, "budget");
    }

    /**
     * 新增预算收款
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增预算收款
     */
    @GetMapping("/add/{stageId}")
    public String add(@PathVariable("stageId") Long stageId, ModelMap mmap)
    {
        mmap.put("alpStageDetail", alpContractStageService.getStageDetail(stageId));
        return prefix + "/addFromStage";
    }
    /**
     * 新增保存预算收款
     */
    @RequiresPermissions("system:budget:add")
    @Log(title = "预算收款", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AlpStageBudget alpStageBudget)
    {
        alpStageBudget.setUpdatedTime(new Date());
        alpStageBudget.setDelFlag(0);
        return toAjax(alpStageBudgetService.insertAlpStageBudget(alpStageBudget));
    }

    /**
     * 修改预算收款
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AlpStageBudget alpStageBudget = alpStageBudgetService.selectAlpStageBudgetById(id);
        mmap.put("alpStageBudget", alpStageBudget);
        return prefix + "/edit";
    }

    /**
     * 修改保存预算收款
     */
    @RequiresPermissions("system:budget:edit")
    @Log(title = "预算收款", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AlpStageBudget alpStageBudget)
    {
        return toAjax(alpStageBudgetService.updateAlpStageBudget(alpStageBudget));
    }

    /**
     * 删除预算收款
     */
    @RequiresPermissions("system:budget:remove")
    @Log(title = "预算收款", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(alpStageBudgetService.deleteAlpStageBudgetByIds(ids));
    }

    /**
     * 可编辑表格--编辑预算金额
     */
    @GetMapping("/editbudget/{stageId}")
    public String editTableBudget(@PathVariable("stageId") Long stageId, ModelMap mmap)
    {
        mmap.put("stageId", stageId);
        return prefix + "/editableBudget";
    }

    /**
     * 查询预算收款列表
     */
    @RequiresPermissions("system:budget:list")
    @PostMapping("/listTable/{stageId}")
    @ResponseBody
    public TableDataInfo listTableActual(@PathVariable("stageId") Long stageId, ModelMap mmap)
    {
        startPage();
        List<AlpStageBudget> list = alpStageBudgetService.selectAlpStageBudgetByStageId(stageId);
        return getDataTable(list);
    }

    /**
     * 修改保存预算收款
     */
    @RequiresPermissions("system:budget:edit")
    @Log(title = "预算收款", businessType = BusinessType.UPDATE)
    @PostMapping("/editOneLine/{id}/{budgetMoney}/{budgetTime}")
    @ResponseBody
    public AjaxResult editOneLine(@PathVariable("id") Long id,
                                  @PathVariable("budgetMoney") BigDecimal budgetMoney,
                                  @PathVariable("budgetTime") Date budgetTime)
    {
        AlpStageBudget budget = alpStageBudgetService.selectAlpStageBudgetById(id); //必须在前
        //重新插入一条
        budget.setId(null);
        if (null != budgetMoney) {
            budget.setBudgetMoney(budgetMoney);
        }
        if (null != budgetTime) {
            budget.setBudgetTime(budgetTime);
        }
        budget.setUpdatedTime(new Date());
        //将原来的记录 置删除标记为1
        alpStageBudgetService.deleteAlpStageBudgetById(id);//必须在后
        return toAjax(alpStageBudgetService.insertAlpStageBudget(budget));
    }

    /**
     * 删除预算收款
     */
    @RequiresPermissions("system:budget:remove")
    @PostMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult removeById(@PathVariable("id") Long id)
    {
        return toAjax(alpStageBudgetService.deleteAlpStageBudgetById(id));
    }

    /**
     * 查询预算收款列表
     */
    @RequiresPermissions("system:contractDetail:moneyBack")
    @PostMapping("/listBudgetTable/{contractId}")
    @ResponseBody
    public TableDataInfo listBudgetTable(@PathVariable("contractId") Long contractId, ModelMap mmap)
    {
        startPage();
        AlpStageBudget alpStageBudget = new AlpStageBudget();
        alpStageBudget.setContractId(contractId);
        List<AlpStageBudget> list = alpStageBudgetService.selectAlpStageBudgetList(alpStageBudget);
        return getDataTable(list);
    }

}
